package com.ia.aistream.controller.dxfcomponent;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.ia.aistream.common.annotation.Log;
import com.ia.aistream.common.core.domain.AjaxResult;
import com.ia.aistream.common.core.page.PageDomain;
import com.ia.aistream.common.core.page.TableDataInfo;
import com.ia.aistream.common.core.page.TableSupport;
import com.ia.aistream.common.enums.BusinessType;
import com.ia.aistream.common.enums.ResponeCodeMsgEnums;
import com.ia.aistream.common.utils.StringUtils;
import com.ia.aistream.common.utils.sql.SqlUtil;
import com.ia.aistream.manager.dxfcomponent.conf.DrawMapConf;
import com.ia.aistream.model.dxfcomponent.dto.DrawDto;
import com.ia.aistream.model.dxfcomponent.entity.DxfComponent;
import com.ia.aistream.model.dxfcomponent.entity.DxfDraw;
import com.ia.aistream.service.dxfcomponent.DrawAnalysisService;
import com.ia.aistream.service.dxfcomponent.IDxfDrawService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * 实现图纸的上传，并实现数据的解析回显
 * @Author xiaoyu
 * @create 2020/12/8 8:49
 */
@Controller
@RequestMapping("/dxfcomponent/cad")
public class DrawAnalysisController {

    private String prefix = "dxfcomponent/cad";

    @Autowired
    private IDxfDrawService dxfDrawService;
    @Autowired
    private DrawAnalysisService drawAnalysisService;

    /**
     * 数据展示页
     * @return
     */
    @RequiresPermissions("dxfcomponent:cad:view")
    @GetMapping()
    public String component()
    {
        return prefix + "/draw";
    }

    /**
     * 根据建立连接的sessionID
     * 查询图纸解析后的数据列表
     */
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(DxfDraw dxfDraw)
    {
//        startPage();
        List<DxfDraw> dxfDrawList = drawAnalysisService.getDxfDraw(dxfDraw);
        if(dxfDrawList==null){
            dxfDrawList = new ArrayList<>();
        }

        return getDataTable(dxfDrawList);
    }

    /**
     * 上传dwg文件，将dwg文件转化为dxf文件
     */
    @Log(title = "dxf文件索引信息", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult uploadFile(MultipartFile[] files) throws Exception {

        // 将上传的文件集合转化为索引信息的集合
        return AjaxResult.success(ResponeCodeMsgEnums.OK.getErrmsg(),drawAnalysisService.getDrawIndexService(files));
    }

    /***
     * 根据查询的索引的信息，
     * @param
     * @return
     */
    @GetMapping("/search")
    public String handlerDwg(String fileId, ModelMap mmap){

        Map<String, Object> map = drawAnalysisService.getDrawComponentService(Long.parseLong(fileId));
        // 获取图片的名称
        String imgPathService = drawAnalysisService.getImgPathService(Long.parseLong(fileId));
        System.out.println(map.get(DrawMapConf.DWG_DXF_INDEX.value));
        Object o = map.get(DrawMapConf.DWG_DXF_INDEX.value);
        // 获取索引信息
        mmap.addAttribute("dwgIndex",map.get(DrawMapConf.DWG_DXF_INDEX.value));

        mmap.addAttribute("fileId", fileId);
        mmap.addAttribute("images", imgPathService);

        return prefix + "/search";
    }

    /****
     * 获取设备的索引信息
     * @param fileId
     * @param mmap
     * @return
     */
    @ResponseBody
    @PostMapping("/component/{fileId}")
    public TableDataInfo getComponent(@PathVariable("fileId") String fileId, ModelMap mmap){
        Map<String, Object> map = drawAnalysisService.getDrawComponentService(Long.parseLong(fileId));

        // 获取索引信息
        List<DxfComponent> componentList = (List<DxfComponent>) map.get(DrawMapConf.DXF_COMPONENT_LIST.value);
        return getDataTable(componentList);
    }

    /***
     * 实现图纸的查看
     * @param fileId
     * @param mmap
     * @return
     */
    @GetMapping("/view")
    public String viewImg(String fileId, ModelMap mmap){
       String imgPathService = drawAnalysisService.getImgPathService(Long.parseLong(fileId));
        mmap.put("images",imgPathService);
       return prefix + "/viewImg";
    }

    /***
     * 将数据保存到数据库中
     * @param drawDto
     * @return
     */
    @ResponseBody
    @PostMapping("/save")
    public AjaxResult saveData(@RequestBody DrawDto drawDto) {
        return toAjax(drawAnalysisService.insertBatchService(drawDto));
    }

    /***
     * 验证索引信息是否已经存在
     * @param dxfDraw
     * @return
     */
    @ResponseBody
    @PostMapping("/check")
    public Integer checkIndex(@RequestBody DxfDraw dxfDraw){
        return dxfDrawService.checkDxfDrawList(dxfDraw).size();
    }

    /***
     * 验证图片是否转化完成
     * @param fileId
     * @return
     */
    @ResponseBody
    @GetMapping("/checkImg")
    public boolean checkImg(Long fileId){
        return drawAnalysisService.checkDxfImg(fileId);
    }
    /**
     * 删除标题栏信息
     * @param ids
     * @return
     */
    @Log(title = "dxf文件索引信息", businessType = BusinessType.DELETE)
    @PostMapping( "/remove")
    @ResponseBody
    public AjaxResult remove(long ids)
    {
        return toAjax(drawAnalysisService.deleteDrawService(ids));
    }

    /**
     * 新增图纸解析数据
     */
    @GetMapping("/add")
    public String add()
    {
        return prefix + "/upload";
    }

    /**
     * 设置请求分页数据
     */
    protected void startPage()
    {
        PageDomain pageDomain = TableSupport.buildPageRequest();
        Integer pageNum = pageDomain.getPageNum();
        Integer pageSize = pageDomain.getPageSize();
        if (StringUtils.isNotNull(pageNum) && StringUtils.isNotNull(pageSize))
        {
            String orderBy = SqlUtil.escapeOrderBySql(pageDomain.getOrderBy());
            PageHelper.startPage(pageNum, pageSize, orderBy);
        }
    }

    /**
     * 响应请求分页数据
     */
    @SuppressWarnings({ "rawtypes", "unchecked" })
    protected TableDataInfo getDataTable(List<?> list)
    {
        TableDataInfo rspData = new TableDataInfo();
        rspData.setCode(0);
        rspData.setRows(list);
        rspData.setTotal(new PageInfo(list).getTotal());
        return rspData;
    }

    /**
     * 响应返回结果
     *
     * @param rows 影响行数
     * @return 操作结果
     */
    protected AjaxResult toAjax(int rows)
    {
        return rows > 0 ? success() : error();
    }

    /**
     * 响应返回结果
     *
     * @param result 结果
     * @return 操作结果
     */
    protected AjaxResult toAjax(boolean result)
    {
        return result ? success() : error();
    }

    /**
     * 返回成功
     */
    public AjaxResult success()
    {
        return AjaxResult.success();
    }

    /**
     * 返回失败消息
     */
    public AjaxResult error()
    {
        return AjaxResult.error();
    }

    /**
     * 返回成功消息
     */
    public AjaxResult success(String message)
    {
        return AjaxResult.success(message);
    }

    /**
     * 返回失败消息
     */
    public AjaxResult error(String message)
    {
        return AjaxResult.error(message);
    }

    /**
     * 返回错误码消息
     */
    public AjaxResult error(AjaxResult.Type type, String message)
    {
        return new AjaxResult(type, message);
    }

}
